import request from "../../utils/request"
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    timer: "",
    minute: 59,
    second: 59,
    modePayment: 0,
    allData: [],
    confirmOrderData: [],
    endTime: '',
    id:"",
    cid:"",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onLoad(option) {
      this.setData({
        endTime: option.time
      })
      this.setData({
        id:option.id
      })
      this.getTeacherClassData(option.id)
      this.setData({
        timer: new Date()
      })
      this.countdown()
    },
    countdown() {
      let m = 1000 * 60
      let endTime = this.data.endTime
      let time = setTimeout(() => {
        let differTime = endTime - new Date().getTime()
        let minute = Math.floor(differTime / m)
        let second = Math.floor((differTime - minute * m) / 1000)
        this.setData({
          minute,
          second
        })
        if (differTime <= 0) {
          this.setData({
            minute: 0,
            second: 0
          })
          request('/users/del', {id:this.data.cid}, {}, 'POST')
          wx.switchTab({
            url: "/pages/home/home",
          })
        }
        if (differTime > 0) {
          this.countdown();
        }

      }, 1000)

    },
    confirmPayment() {
      let that = this
      wx.showModal({
        title: '付款详情',
        content: `￥${this.data.confirmOrderData.classPrice}\r\n支付方式：${this.data.modePayment === 0?"微信":"支付宝"} `,
        showCancel: true,
        confirmColor: "#FF3F3F",
        success(res) {
          if (res.confirm) {
            that.addOrders(true)
            wx.navigateTo({
              url: `/pages/PaymentSuccess/PaymentSuccess`,
            })
          } else if (res.cancel) {
            that.addOrders(false)
            console.log('用户点击取消')
          }
        }
      })
    },
    payMentWx() {
      this.setData({
        modePayment: 0
      })
    },
    payMentZfb() {
      this.setData({
        modePayment: 1
      })
    },
    // 获取teacherClassList 所有数据
    async getTeacherClassData(id, params) {
      await request('/users/lecture', params, {}, 'GET').then(res => {
        res.map((item) => {
          item.teacherClassList = JSON.parse(item.teacherClassList)
        })
        let arr = [];
        res.forEach((item) => {
          arr.push(...item.teacherClassList)
        })
        this.setData({
          allData: arr
        })
        let obj = this.data.allData.find(item => {
          item.num = 1
          return item.cid === id
        })
        this.setData({
          confirmOrderData: obj
        })
        console.log(this.data.confirmOrderData);
      })
    },
    //添加订单
    async addOrders(paymentState) {
      let obj = {
        cid: new Date().getTime(),
        teacher: "天天",
        userTel: "15679821243",
        classTitle: this.data.confirmOrderData.classTitle,
        classPrice: this.data.confirmOrderData.classPrice,
        orderFlag: "已处理",
        paymentStatus: paymentState,
        PaymentTime: new Date().toLocaleDateString()
      }
      this.setData({
        cid:obj.cid
      })
      await request('/users/addOrder', obj, {}, 'POST')
    },
  }
})